﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;


/*
 * TH07 Groep 10
 * ----------------------------------------------------------------------------------
 * Versie   Door        Commentaar
 * 0.1      Mike        Opzet en programmeer werk
 * 0.2      Luca        Verplaatst naar Algorithms, 
 *                      aangepast om als reference te gebruiken
 */

namespace Algorithms_10.Tools
{
    public class Crop
    {
        public Crop() { }

        public Bitmap run(VisionImage img)
        {

            int width = img.Width;
            int height = img.Height;

            int firstX = -1;
            int firstY = -1;

            int lastX = -1;
            int lastY = -1;

            int cropWidth;
            int cropHeight;


            img.Lock();
            int offset = img.Data.Stride - (width * 3);
            unsafe
            {
                byte* charPointer = (byte*)img.Data.Scan0;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        //Is black pixel
                        if (charPointer[0] == 0 && charPointer[1] == 0 && charPointer[2] == 0)
                        {
                            if (firstX == -1)
                            {
                                firstX = x;
                            }
                            else if (x < firstX)
                            {
                                firstX = x;
                            }

                            if (firstY == -1)
                            {
                                firstY = y;
                            }
                            else if (y < firstY)
                            {
                                firstY = y;
                            }

                            if (lastX == -1)
                            {
                                lastX = x;
                            }
                            else if (x > lastX)
                            {
                                lastX = x;
                            }

                            if (lastY == -1)
                            {
                                lastY = y;
                            }
                            else if (y > lastY)
                            {
                                lastY = y;
                            }

                        }
                        charPointer += 3;
                    }
                    charPointer += offset;
                }
            }
            img.Unlock();

            cropWidth = lastX - firstX;
            cropHeight = lastY - firstY;

            Rectangle rect = new Rectangle(firstX, firstY, cropWidth, cropHeight);
            Bitmap croppedImg = img.Bitmap.Clone(rect, img.Bitmap.PixelFormat);
            return croppedImg;

        }
    }
}
